package com.xz.db.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * MyMetaObjectHandler
 *
 * @author 28310
 * @since 2023-11-14 13:31
 */
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasGetter("createdTime")) {
            this.setFieldValByName("createdTime", LocalDateTime.now(), metaObject);
        }
        if (metaObject.hasGetter("updatedTime")) {
            this.setFieldValByName("updatedTime", LocalDateTime.now(), metaObject);
        }
        //做创建用户或者修改用户
//        ICurrentOperator operator = CurrentOperatorHolder.get();
//        if (operator != null && operator.getInfo()!=null) {
//            if (metaObject.hasGetter("createById")) {
//                this.strictInsertFill(metaObject, "createById", Long.class, operator.getInfo().getAccountId());
//            }
//            if (metaObject.hasGetter("createBy")) {
//                this.strictInsertFill(metaObject, "createBy", String.class,  operator.getInfo().getName());
//            }
//            if (metaObject.hasGetter("updateById")) {
//                this.strictInsertFill(metaObject, "updateById", Long.class, operator.getInfo().getAccountId());
//            }
//            if (metaObject.hasGetter("updateBy")) {
//                this.strictInsertFill(metaObject, "updateBy", String.class,  operator.getInfo().getName());
//            }
//        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasGetter("updatedTime")) {
            this.setFieldValByName("updatedTime", LocalDateTime.now(), metaObject);
        }
//        ICurrentOperator operator = CurrentOperatorHolder.get();
//        if (operator != null && operator.getInfo()!=null) {
//            if (metaObject.hasGetter("updateById")) {
//                this.strictInsertFill(metaObject, "updateById", Long.class,  operator.getInfo().getAccountId());
//            }
//            if (metaObject.hasGetter("updateBy")) {
//                this.strictInsertFill(metaObject, "updateBy", String.class,  operator.getInfo().getName());
//            }
//        }
    }
}
